package com.licq.ldoc.domain.support;

import java.util.HashMap;
import java.util.Map;

public abstract class ToStringBehavior {
	private static Map<Class<?>, ToStringBehavior> behaviors = 
		new HashMap<Class<?>, ToStringBehavior>();
	
	static {
		registerBehavior(new DefaultToStringBehavior());
		registerBehavior(new CalendarToStringBehavior());
		registerBehavior(new ClassToStringBehavior());
		registerBehavior(new DateToStringBehavior());
		registerBehavior(new DecimalToStringBehavior());
	}
	
	public static void registerBehavior(ToStringBehavior behavior) {
		behaviors.put(behavior.getClass(), behavior);
	}

	public String toString(Object obj) {
		if (obj == null) {
			return null;
		} else {
			return doToString(obj);
		}
	}

	protected abstract String doToString(Object obj);
	
	public static ToStringBehavior defaut() {
		return behaviors.get(DefaultToStringBehavior.class);
	}
	
	public static ToStringBehavior date() {
		return behaviors.get(DateToStringBehavior.class);
	}
	
	public static ToStringBehavior clazz() {
		return behaviors.get(ClassToStringBehavior.class);
	}
	
	public static ToStringBehavior calendar() {
		return behaviors.get(CalendarToStringBehavior.class);
	}
	
	public static ToStringBehavior decimal() {
		return behaviors.get(DecimalToStringBehavior.class);
	}
	
}
